import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutterdemo/0demo/bloc/cubit/counter_cubit.dart';

class CounterView extends StatelessWidget {
  const CounterView({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => CounterCubit(),
      child: BlocBuilder<CounterCubit, CounterState>(
        builder: (context, counterState) => _buildPage(context, counterState),
      ),
    );
  }

  Scaffold _buildPage(BuildContext context, CounterState counterState) {
    // final cubit = BlocProvider.of<CounterCubit>(context);
    return Scaffold(
      appBar: AppBar(title: Text("cubit test")),
      body: Stack(
        children: [
          Align(
            alignment: Alignment.center,
            // child: Text("count: ${cubit.state.count}"),
            child: Text("count: ${counterState.count}"),
          ),
          Align(
            alignment: Alignment.bottomRight,
            child: Container(
              padding: EdgeInsets.all(50),
              child: FloatingActionButton(
                onPressed: () {
                  // cubit.increment();
                  context.read<CounterCubit>().increment();
                },
                child: Icon(Icons.add),
              ),
            ),
          )
        ],
      ),
    );
  }
}
